import Promise from "../promise";

let promise = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve("成功了！");
        // reject("失败了！");
    }, 1000);
    // 同步，实际开发中这种情况很少，Promise设计的初衷就是用来处理异步的，但是我们框架里还是要支持同步的。
    // resolve("成功了！");
    // reject("失败了！");
});

// 用于测试catch方法
promise.then((resolveData1) => {
    console.log("第1个then执行成功:", resolveData1);
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            // resolve("第二个异步操作执行成功!");
            reject("第二个异步操作执行失败!");
        }, 1000);
    });
}, (rejectData1) => {
    console.log("第1个then执行失败:", rejectData1);
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            // resolve("第二个异步操作执行成功!");
            reject("第二个异步操作执行失败!");
        }, 1000);
    });
}).then((resolveData2) => {
    console.log("第2个then执行成功:", resolveData2);
    return "ok2";
}).catch((err) => { // catch一般放到末尾，如果放到中间则作用和then是一样的.
    console.log("执行catch:", err);
    return "catched"
}).then((resolveData3) => {
    console.log("第3个then执行成功:", resolveData3);
}, (rejectData3) => {
    console.log("第3个then执行失败:", rejectData3);
})
